# Start with a rust alpine image
FROM rust:1-alpine3.15
# This is important, see https://github.com/rust-lang/docker-rust/issues/85
ENV RUSTFLAGS="-C target-feature=-crt-static"
# apk切换中科大镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk update
# 安装编译依赖
RUN apk add --no-cache musl-dev
# rust diesel限制需要安装mysql连接驱动
RUN apk add --no-cache mariadb-connector-c-dev
# 将编译需要用到的库导出到环境
RUN LIBRARY_PATH=/usr/local/rustup/toolchains/1.61.0-aarch64-unknown-linux-musl/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/
RUN export LIBRARY_PATH

WORKDIR /app
COPY ./ /app
# 编译程序
RUN cargo build --release
RUN strip target/release/axum_test

# use a plain alpine image, the alpine version needs to match the builder
FROM alpine:3.15
# apk切换中科大镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk update
RUN apk add --no-cache libgcc
# rust diesel限制需要安装mysql连接驱动
RUN apk add --no-cache mariadb-connector-c-dev
# 拷贝二进制文件
COPY --from=0 /app/target/release/axum_test /user/local/bin/
# 配置文件复制到运行目录下
# COPY --from=0 /app/application.yml /user/local/bin/
COPY ./application.yml /user/local/bin/
# set the binary as entrypoint
# 映射3000端口
EXPOSE 3000
# 切换运行目录
WORKDIR /user/local/bin/
# 运行程序
ENTRYPOINT ["./axum_test"]
